package com.bjpowernode.crm.web.controller;

import com.bjpowernode.crm.constants.Constants;
import com.bjpowernode.crm.pojo.Clue;
import com.bjpowernode.crm.pojo.Page;
import com.bjpowernode.crm.pojo.Transaction;
import com.bjpowernode.crm.pojo.User;
import com.bjpowernode.crm.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("clue")
public class ClueController {

    @Autowired
    private ClueService clueService;

    @GetMapping("page.json")
    public Page page(Page page, @RequestParam Map map) {
        System.out.println(map);
        clueService.getPage(page, map);
        return page;
    }

    @GetMapping("clueActivities.json")
    public List clueActivities(String clueId) {
        return clueService.getClueActivities(clueId);
    }

    @DeleteMapping("unRel.do")
    public Map unRel(String id) {
        clueService.unRel(id);
        return Constants.Result.SUC_WITH_MSG;
    }

    @PostMapping("rel.do")
    public Map rel(String clueId, String[] actIds) {
        clueService.rel(clueId, actIds);
        return Constants.Result.SUC_WITH_MSG;
    }

    @GetMapping("clue.json")
    public Clue clue(String id) {
        return clueService.getClue(id);
    }

    @PostMapping("convert.do")
    public Map convert(String clueId, boolean createTran, Transaction transaction, HttpSession session) {

        User user = (User) session.getAttribute(Constants.LOGIN_USER);
        String createBy = user.getLoginAct() + "|" + user.getName();

        clueService.convert(clueId, transaction, createTran, createBy);

        return Constants.Result.SUC_WITH_MSG;
    }
}
